package com.zd.controller;

import com.github.pagehelper.PageHelper;
import com.zd.domain.Post;
import com.zd.domain.PostImgs;
import com.zd.domain.Result;
import com.zd.service.MinioService;
import com.zd.service.post.PostService;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;

    @Autowired
    private MinioService minioService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    @GetMapping("/myPosts")
    public Result<List<Post>> myPosts(@RequestParam("userId") Integer userId){

        Result<List<Post>> result = new Result<>();

        if (userId != 0){
            List<Post> posts = postService.myPosts(userId);


            if ((posts != null) && (posts.size() > 0)){
                result.setCode(200);
                result.setMsg("查询成功！");
                result.setData(posts);
            }else {
                result.setCode(400);
                result.setMsg("暂无数据！");
            }

        }

        return result;
    }

    @PutMapping("/deletePost")
    public Result deletePost(@RequestParam("postId") Integer postId){

        Result result = new Result();

        if (postId != 0){
            int i = postService.updatePostState(postId);

            if (i == 1){
                result.setCode(200);
                result.setMsg("删除成功！");
            }else {
                result.setCode(400);
                result.setMsg("删除失败！");
            }

        }

        return result;
    }

    @GetMapping("/postList")
    public Result<List<Post>> postList(){

        Result<List<Post>> result = new Result<>();



            List<Post> list = postService.list();

            if (list != null && list.size() != 0){
                result.setCode(200);
                result.setMsg("查询成功！");
                result.setData(list);
            }else {
                result.setCode(400);
                result.setMsg("暂无数据！");
            }


        return result;

    }

    @PostMapping("/savePost")
    public Result savePost(@RequestBody Post post){

        Result result = new Result();

        if (post != null){

            int i = postService.savePost(post);

            if (i != 0){
                result.setCode(200);
                result.setMsg("添加成功！");
            }else {
                result.setCode(400);
                result.setMsg("添加失败！");
            }


        }else {
            result.setCode(401);
            result.setMsg("添加失败，前端无数据传输！");
        }

        return result;

    }


//    @PostMapping("savePostAndImgs")
//    public Result savePostAndImgs(@RequestParam("file") MultipartFile[] files,
//                                  @RequestParam("userId") int userId,
//                                  @RequestParam("postContent") String postContent) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
//
//        Result result = new Result();
//        int i = 0;
//        Post post = new Post();
//        post.setUserId(userId);
//        //都存在
//        if ((files != null) && (postContent != null)){
//            System.out.println(userId);
//            System.out.println(postContent);
//
//            post.setPostContent(postContent);
//            List<PostImgs> postImgsList = new ArrayList<>();
//
//            //添加图片和文字
//            for (MultipartFile file : files) {
//
//                String url = minioService.uploadFile("video", file);
//
//                System.out.println(url);
//                if (url != null){
//                    //将图片地址存到；postImgs
//                    PostImgs postImgs1 = new PostImgs();
//                    postImgs1.setPostImgAddress(url);
//
//                    postImgsList.add(postImgs1);
//                }
//
//            }
//
//            if (postImgsList != null && postImgsList.size() > 0){
//                post.setPostImgsList(postImgsList);
//
//                System.out.println("添加图片和文字=============");
//                i = postService.savePostAndImgs(post);
//
//                if (i > 0){
//                    System.out.println("添加图片和文字成功！");
//                    result.setCode(200);
//                    result.setMsg("添加图片和文字成功！");
//                }else {
//                    result.setCode(400);
//                    result.setMsg("添加图片和文字失败");
//                }
//
//            }
//
//        } else if ((files == null) && (postContent != null)) {
//            //只添加文字
//            post.setPostContent(postContent);
//
//            System.out.println("添加贴子内容===========");
//            i = postService.savePost(post);
//
//            if (i > 0){
//                result.setCode(201);
//                result.setMsg("添加贴子内容成功！");
//                System.out.println("添加贴子内容成功！");
//            }else {
//                result.setCode(402);
//                result.setMsg("添加贴子内容失败！");
//            }
//
//        }else {
//            result.setCode(401);
//            result.setMsg("添加失败！");
//        }
//
//
//        return result;
//
//
//
//    }

    @GetMapping("findPost")
    public Result<Post> findPost(@RequestParam("postId") Integer postId){

        Result<Post> result = new Result<>();

        Post post = postService.findPost(postId);

        if (post != null){
            result.setCode(200);
            result.setMsg("查询成功！");
            result.setData(post);
        }else {
            result.setCode(400);
            result.setMsg("暂无数据！");
        }

        return result;

    }

}
